//
//  YDUserGuideViewController.m
//  IngCard
//
//  Created by kf on 2017/8/2.
//  Copyright © 2017年 IngDan. All rights reserved.
//

#import "YDUserGuideViewController.h"
#import "UIImage+YYWebImage.h"
#import "YDUserRegisterViewController.h"
#import "YDUserLoginViewController.h"
#import "LinkedInHelper.h"
#import "YD3rdInfoConfirmViewController.h"
#import "YDAccountManager.h"
#import "YDAuthManager.h"
#import "YD3rdRegisterViewController.h"
#import "YD3rdInfoConfirmViewController.h"
#import "YDUserBaseInfoEditViewController.h"
#import "YDUserExperienceEditViewController.h"

@interface YDUserGuideViewController ()<UINavigationControllerDelegate>

@property(nonatomic, strong) UIButton *registerBtn;
@property(nonatomic, strong) UIButton *loginBtn;

@property(nonatomic, strong) UIImageView *guideIndex1;
@property(nonatomic, strong) UIImageView *guideIndex2;


@property(nonatomic, strong) UIButton *linkedInButton;
@property(nonatomic, strong) UIButton *weChatButton;
@property(nonatomic, strong) UIButton *weiboButton;

@end

@implementation YDUserGuideViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.delegate = self;
}

- (void) initSubviews
{
    [super initSubviews];
    
    UIImageView *bgImgView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    bgImgView.image = UIImageMake(@"UserGuide");
    [self.view addSubview:bgImgView];
    
    [self.view addSubview:self.weChatButton];
    [self.weChatButton makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(-46);
        make.centerX.equalTo(self.view);
    }];
    
    [self.view addSubview:self.linkedInButton];
    [self.linkedInButton makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.weChatButton.mas_left).offset(-40);
        make.centerY.equalTo(self.weChatButton);
    }];
    
    [self.view addSubview:self.weiboButton];
    [self.weiboButton makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.weChatButton.mas_right).offset(40);
        make.centerY.equalTo(self.weChatButton);
    }];
    
    UIImageView *guideIndex4 = [[UIImageView alloc] initWithImage:UIImageMake(@"guideIndex4")];
    [self.view addSubview:guideIndex4];
    [guideIndex4 makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.weChatButton.mas_top).offset(-56);
    }];
    
    [self.view addSubview:self.loginBtn];
    [self.loginBtn makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.weChatButton.mas_top).offset(-56);
    }];
    
    UIImageView *guideIndex3 = [[UIImageView alloc] initWithImage:UIImageMake(@"guideIndex3")];
    [self.view addSubview:guideIndex3];
    [guideIndex3 makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.loginBtn.mas_top).offset(-20);
    }];
    
    [self.view addSubview:self.registerBtn];
    [self.registerBtn makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.loginBtn.mas_top).offset(-20);
    }];
    
    UIImageView *guideIndex2 = [[UIImageView alloc] initWithImage:UIImageMake(@"guideIndex2")];
    [guideIndex2 sizeToFit];
    [self.view addSubview:guideIndex2];
    [guideIndex2 makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.registerBtn.mas_top).offset(-37);
    }];
    
    UIImageView *guideIndex1 = [[UIImageView alloc] initWithImage:UIImageMake(@"guideIndex1")];
    [guideIndex1 sizeToFit];
    [self.view addSubview:guideIndex1];
    [guideIndex1 makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(guideIndex2.mas_top).offset(60);
    }];
}

#pragma mark - UINavigationControllerDelegate
// 将要显示控制器
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 判断要显示的控制器是否是自己
    BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
    
    [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
}

#pragma mark - private
- (void)regsterBtnAction:(UIButton *)btn
{
    YDUserRegisterViewController *registerController = [[YDUserRegisterViewController alloc] init];
    [self.navigationController pushViewController:registerController animated:YES];
}

- (void)loginBtnAction:(UIButton *)btn
{
    YDUserLoginViewController *loginController = [[YDUserLoginViewController alloc] init];
    [self.navigationController pushViewController:loginController animated:YES];
}

- (void)linkedInBtnAction:(UIButton *)btn
{
    LinkedInHelper *linkedIn = [LinkedInHelper sharedInstance];
    
    
    if (linkedIn.isValidToken)
    {
        [linkedIn autoFetchUserInfoWithSuccess:^(NSDictionary *userInfo) {
            [self linkedInCallback];
        } failUserInfo:^(NSError *error) {
            NSLog(@"error : %@", error.userInfo.description);
        }];
    }
    else
    {
        linkedIn.cancelButtonText = @"close";
        linkedIn.showActivityIndicator = YES;
        
        NSArray *permissions = @[@(BasicProfile),
                                 @(EmailAddress),
                                 @(Share),
                                 @(CompanyAdmin)];
        
        [linkedIn requestMeWithSenderViewController:self
                                           clientId:@"81ucrztw31nbz7"         // Your App Client Id
                                       clientSecret:@"miTOoguIOWl7VzR2"         // Your App Client Secret
                                        redirectUrl:@"http://devsvcard.ingdan.com/auth/callback"         // Your App Redirect Url
                                        permissions:permissions
                                              state:@""               // Your client state
                                    successUserInfo:^(NSDictionary *userInfo) {
                                        // Whole User Info
                                        [self linkedInCallback];
                                    }
                                  failUserInfoBlock:^(NSError *error) {
                                      NSLog(@"error : %@", error.userInfo.description);
                                  }
         ];
    }
    
}

- (void)wechatBtnAction:(UIButton *)btn
{
    YDUserExperienceEditViewController *vc = [[YDUserExperienceEditViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)weiboBtnAction:(UIButton *)btn
{
    YDUserBaseInfoEditViewController *loginController = [[YDUserBaseInfoEditViewController alloc] init];
    [self.navigationController pushViewController:loginController animated:YES];
}

-(void) linkedInCallback
{
    [AUTH authCheckBinding:(YD3rdSignID_LinkedIn)
              accessToken:[LinkedInHelper sharedInstance].accessToken
               onComplete:^(BOOL success, NSString *msg)
    {
        if (success)
        {
            [QMUITips showInfo:msg inView:self.view hideAfterDelay:2];
        }
        else
        {
            YD3rdRegisterViewController *vc = [[YD3rdRegisterViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }
    }];
}
#pragma mark - lazy loading

-(UIButton *) registerBtn
{
    if (!_registerBtn)
    {
        _registerBtn = [[UIButton alloc] init];
        _registerBtn.frame = CGRectMake(0, 0, 232, 46);
        _registerBtn.layer.cornerRadius = 23;
        _registerBtn.layer.masksToBounds = YES;
        [_registerBtn setStyle:^(UIView *target) {
            UIBUTTONSTYLE;
            [temp setTitle:STRING.user_signUp forState:UIControlStateNormal];
            
            [temp setBackgroundImage:UIImageMake(@"guideIndex3") forState:(UIControlStateNormal)];
            
            temp.titleLabel.font = STYLE.roundButton.font;
            temp.tintColor = STYLE.roundButton.color;
            [temp setTitleColor:UIColorMake(255, 80, 0) forState:UIControlStateSelected];
        }];
        [_registerBtn addTarget:self
                         action:@selector(regsterBtnAction:)
               forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _registerBtn;
}

-(UIButton *) loginBtn
{
    if (!_loginBtn)
    {
        _loginBtn = [[UIButton alloc] init];
        _loginBtn.frame = CGRectMake(0, 0, 232, 46);
        _loginBtn.layer.cornerRadius = 23;
        _loginBtn.layer.masksToBounds = YES;
        
        
        [_loginBtn setStyle:^(UIView *target) {
            UIBUTTONSTYLE;
            [temp setTitle:STRING.user_signIn forState:UIControlStateNormal];
            [temp setBackgroundImage:UIImageMake(@"guideIndex4") forState:(UIControlStateNormal)];
            
            temp.titleLabel.font = STYLE.roundButton.font;
            temp.tintColor = STYLE.roundButton.color;
            
            [temp setTitleColor:UIColorMake(255, 80, 0) forState:UIControlStateSelected];
        }];
        
        [_loginBtn addTarget:self
                      action:@selector(loginBtnAction:)
            forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _loginBtn;
}


-(UIButton *) linkedInButton
{
    if (!_linkedInButton)
    {
        _linkedInButton = [[UIButton alloc] init];
        [_linkedInButton setBackgroundImage:UIImageMake(@"user_3rd_linkedin") forState:UIControlStateNormal];
        [_linkedInButton sizeToFit];
        [_linkedInButton addTarget:self
                            action:@selector(linkedInBtnAction:)
                  forControlEvents:UIControlEventTouchUpInside];
    }
    return _linkedInButton;
}

-(UIButton *) weChatButton
{
    if (!_weChatButton)
    {
        _weChatButton = [[UIButton alloc] init];
        [_weChatButton setBackgroundImage:UIImageMake(@"user_3rd_wechat") forState:UIControlStateNormal];
        [_weChatButton sizeToFit];
        [_weChatButton addTarget:self
                          action:@selector(wechatBtnAction:)
                forControlEvents:UIControlEventTouchUpInside];
    }
    return _weChatButton;
}

-(UIButton *) weiboButton
{
    if (!_weiboButton)
    {
        _weiboButton = [[UIButton alloc] init];
        [_weiboButton setBackgroundImage:UIImageMake(@"user_3rd_weibo") forState:UIControlStateNormal];
        [_weiboButton sizeToFit];
        [_weiboButton addTarget:self
                         action:@selector(weiboBtnAction:)
               forControlEvents:UIControlEventTouchUpInside];
    }
    return _weiboButton;
}
@end

